package com.mj.haier.calc.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.calc.domain.SettlementProcessInfo;
import com.mj.haier.calc.repository.SettlementProcessInfoRepository;
import com.mj.haier.calc.repository.search.SettlementProcessInfoSearchRepository;
import com.mj.haier.calc.web.rest.errors.BadRequestAlertException;
import com.mj.haier.calc.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing SettlementProcessInfo.
 */
@RestController
@RequestMapping("/api")
public class SettlementProcessInfoResource {

    private final Logger log = LoggerFactory.getLogger(SettlementProcessInfoResource.class);

    private static final String ENTITY_NAME = "microCalcSettlementProcessInfo";

    private final SettlementProcessInfoRepository settlementProcessInfoRepository;

    private final SettlementProcessInfoSearchRepository settlementProcessInfoSearchRepository;

    public SettlementProcessInfoResource(SettlementProcessInfoRepository settlementProcessInfoRepository, SettlementProcessInfoSearchRepository settlementProcessInfoSearchRepository) {
        this.settlementProcessInfoRepository = settlementProcessInfoRepository;
        this.settlementProcessInfoSearchRepository = settlementProcessInfoSearchRepository;
    }

    /**
     * POST  /settlement-process-infos : Create a new settlementProcessInfo.
     *
     * @param settlementProcessInfo the settlementProcessInfo to create
     * @return the ResponseEntity with status 201 (Created) and with body the new settlementProcessInfo, or with status 400 (Bad Request) if the settlementProcessInfo has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/settlement-process-infos")
    @Timed
    public ResponseEntity<SettlementProcessInfo> createSettlementProcessInfo(@Valid @RequestBody SettlementProcessInfo settlementProcessInfo) throws URISyntaxException {
        log.debug("REST request to save SettlementProcessInfo : {}", settlementProcessInfo);
        if (settlementProcessInfo.getId() != null) {
            throw new BadRequestAlertException("A new settlementProcessInfo cannot already have an ID", ENTITY_NAME, "idexists");
        }
        SettlementProcessInfo result = settlementProcessInfoRepository.save(settlementProcessInfo);
        settlementProcessInfoSearchRepository.save(result);
        return ResponseEntity.created(new URI("/api/settlement-process-infos/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /settlement-process-infos : Updates an existing settlementProcessInfo.
     *
     * @param settlementProcessInfo the settlementProcessInfo to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated settlementProcessInfo,
     * or with status 400 (Bad Request) if the settlementProcessInfo is not valid,
     * or with status 500 (Internal Server Error) if the settlementProcessInfo couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/settlement-process-infos")
    @Timed
    public ResponseEntity<SettlementProcessInfo> updateSettlementProcessInfo(@Valid @RequestBody SettlementProcessInfo settlementProcessInfo) throws URISyntaxException {
        log.debug("REST request to update SettlementProcessInfo : {}", settlementProcessInfo);
        if (settlementProcessInfo.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        SettlementProcessInfo result = settlementProcessInfoRepository.save(settlementProcessInfo);
        settlementProcessInfoSearchRepository.save(result);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, settlementProcessInfo.getId().toString()))
            .body(result);
    }

    /**
     * GET  /settlement-process-infos : get all the settlementProcessInfos.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of settlementProcessInfos in body
     */
    @GetMapping("/settlement-process-infos")
    @Timed
    public List<SettlementProcessInfo> getAllSettlementProcessInfos() {
        log.debug("REST request to get all SettlementProcessInfos");
        return settlementProcessInfoRepository.findAll();
    }

    /**
     * GET  /settlement-process-infos/:id : get the "id" settlementProcessInfo.
     *
     * @param id the id of the settlementProcessInfo to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the settlementProcessInfo, or with status 404 (Not Found)
     */
    @GetMapping("/settlement-process-infos/{id}")
    @Timed
    public ResponseEntity<SettlementProcessInfo> getSettlementProcessInfo(@PathVariable Long id) {
        log.debug("REST request to get SettlementProcessInfo : {}", id);
        Optional<SettlementProcessInfo> settlementProcessInfo = settlementProcessInfoRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(settlementProcessInfo);
    }

    /**
     * DELETE  /settlement-process-infos/:id : delete the "id" settlementProcessInfo.
     *
     * @param id the id of the settlementProcessInfo to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/settlement-process-infos/{id}")
    @Timed
    public ResponseEntity<Void> deleteSettlementProcessInfo(@PathVariable Long id) {
        log.debug("REST request to delete SettlementProcessInfo : {}", id);

        settlementProcessInfoRepository.deleteById(id);
        settlementProcessInfoSearchRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/settlement-process-infos?query=:query : search for the settlementProcessInfo corresponding
     * to the query.
     *
     * @param query the query of the settlementProcessInfo search
     * @return the result of the search
     */
    @GetMapping("/_search/settlement-process-infos")
    @Timed
    public List<SettlementProcessInfo> searchSettlementProcessInfos(@RequestParam String query) {
        log.debug("REST request to search SettlementProcessInfos for query {}", query);
        return StreamSupport
            .stream(settlementProcessInfoSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .collect(Collectors.toList());
    }

}
